<template>
  <div class="login-wrap">
    <el-form
      class="login-form"
      label-position="top"
      label-width="80px"
      :model="queryLoginInfo">
      <h2>用户登录</h2>
      <el-form-item label="用户名">
        <el-input v-model="queryLoginInfo.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="queryLoginInfo.password"></el-input>
      </el-form-item>
      <el-button class="login-btn" type="primary" @click="Login()">登录</el-button>
    </el-form>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        queryLoginInfo:{
          username:'',
          password:'',
        },
        showHomeImag:true
      }
    },
    created(){
      this.$store.state.isHomeRouter = true
      // 页面载入时本地存储中有 state 则用本地state赋值到 vuex 中的 state
      sessionStorage.getItem('state') && this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem('state'))));
      //进行页面刷新时存储 store.state 数据存储到sessionStorage中
      window.addEventListener('beforeunload', () => {
        sessionStorage.setItem('state',JSON.stringify(this.$store.state));
      })
      //打印
      console.log(this.$store.state.isHomeRouter,'页面刷新时候的值');
      //测试
      let list1 = ["a","b","c"];
      let list2 = ["g","q","a","p","c"];
      this.findRestaurant(list1,list2)
    },
    watch:{
      //state值监听isHomeRouter
      "$store.state.isHomeRouter":function (val) {
        console.log(val,'state值监听isHomeRouter')
      }
    },
    methods:{
      //测试方法
    findRestaurant(list1, list2) {
    let arr = [];
    let map = new Map();
    // let map2 = {};
    let obj = {};
    let min = Infinity;
    list1.forEach((item,index)=>{
        map.set(item,index)
    })
    list2.forEach((item,index)=>{
        if(map.get(item)!==undefined){
            let sum = map.get(item)+index
            // map2[item] = map.get(item)+index
            // map2.set(item,map.get(item)+index)
            obj[item] = map.get(item)+index
            if(sum<min){
                min = sum
            }
        }
    })
    for(const key in obj){
        if(obj[key]===min){
            arr.push(key)
        }
    }
    console.log("处理map:",map);
    console.log("处理obj:",obj);
    console.log("处理arr:",arr)
    return arr
},
      //登录请求
      Login(){
          if(this.queryLoginInfo.username===''){
              // this.$alert('请输入用户名', '提示', {
              //   confirmButtonText: '确定',
              //   });
            this.$message({
              message: '请输入用户名',
              type: 'warning'
            });
            return
          }
          if(this.queryLoginInfo.password===''){
              // this.$alert('请输入密码', '提示', {
              //   confirmButtonText: '确定',
              //   });
            this.$message({
              message: '请输入密码',
              type: 'warning'
            });
            return
          }
           /**
           * 成功
           * 0.保存用户token
           *1.跳转home
           * 2.提示成功
           * 失败
           * 1.提示信息
           *
          * */
          this.$fetchGet('/api/login').then(res=>{
          console.log(res,'测试接口数据获取')
          //校验用户名和密码是否正确
          if(this.queryLoginInfo.username!==res.res.userName||this.queryLoginInfo.password!==res.res.passWord){
              // this.$alert('用户名和密码有误请重新输入', '提示', {
              //   confirmButtonText: '确定',
              //   });
            this.$message.error('用户名和密码有误请重新输入');
            return
          }else{
              if(res.status===200){
                //保存token
                this.$store.state.isHomeRouter = true
                localStorage.setItem('token',res.token)
                // this.$router.push("/home");
                const showFlag = this.showHomeImag
                this.$router.push({
                  name:'home',
                  params:{
                    showFlag
                  }
                })
              }else{
                  this.$alert('登录异常', '提示', {
                confirmButtonText: '确定',
                });
              }
          }
      })
      }
    }
  }
</script>
<style>
  .login-wrap{
    height: 100%;
    /*background-color: #151A3C;*/
    /*弹性盒布局：让登录窗口居中css*/
    display: flex;
    justify-content: center;
    align-items: center;
    /*opacity:0.6;*/
    /* margin-top: 10%; */
    /*让登录窗口居中css*/
  }
  .login-wrap .login-form{
    width: 400px;
    background-color: #fff;
    border-radius: 5px;
    padding: 30px;
  }
  .login-wrap .login-btn{
    width: 100%;
  }
</style>
